package org.eiahe.hr.training.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.training.domain.bo.HrTrainingCourseBo;
import org.eiahe.hr.training.domain.vo.HrTrainingCourseVo;
import org.eiahe.hr.training.service.IHrTrainingCourseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 培训课程Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/training/course")
public class HrTrainingCourseController extends BaseController {

    private final IHrTrainingCourseService courseService;

    /**
     * 查询培训课程列表
     */
    @SaCheckPermission("hrTraining:course:list")
    @GetMapping("/list")
    public TableDataInfo<HrTrainingCourseVo> list(HrTrainingCourseBo bo, PageQuery pageQuery) {
        return courseService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取培训课程详细信息
     */
    @SaCheckPermission("hrTraining:course:query")
    @GetMapping("/{courseId}")
    public R<HrTrainingCourseVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long courseId) {
        return R.ok(courseService.queryById(courseId));
    }

    /**
     * 获取课程详情(含课件和学员)
     */
    @SaCheckPermission("hrTraining:course:query")
    @GetMapping("/detail/{courseId}")
    public R<HrTrainingCourseVo> getDetail(@NotNull(message = "主键不能为空") @PathVariable Long courseId) {
        return R.ok(courseService.getCourseDetail(courseId));
    }

    /**
     * 新增培训课程
     */
    @SaCheckPermission("hrTraining:course:add")
    @Log(title = "新增培训课程", linkType = "course", businessType = BusinessType.INSERT, nameField = "courseName")
    @PostMapping
    public R<String> add(@Validated(AddGroup.class) @RequestBody HrTrainingCourseBo bo) {
        String s = courseService.insertByBo(bo);
        return R.ok(s);
    }

    /**
     * 修改培训课程
     */
    @SaCheckPermission("hrTraining:course:edit")
    @Log(title = "培训课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HrTrainingCourseBo bo) {
        return toAjax(courseService.updateByBo(bo));
    }

    /**
     * 删除培训课程
     */
    @SaCheckPermission("hrTraining:course:remove")
    @Log(title = "培训课程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{courseIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] courseIds) {
        return toAjax(courseService.deleteByIds(Arrays.asList(courseIds)));
    }

    /**
     * 为课程添加课件
     */
    @SaCheckPermission("hrTraining:course:edit")
    @PostMapping("/{courseId}/material")
    public R<Void> addMaterial(@PathVariable Long courseId, @RequestBody List<Long> materialIds) {
        return toAjax(courseService.addCourseMaterial(courseId, materialIds));
    }

    /**
     * 为课程添加学员
     */
    @SaCheckPermission("hrTraining:course:edit")
    @PostMapping("/{courseId}/student")
    public R<Void> addStudent(@PathVariable Long courseId, @RequestBody List<Long> employeeIds) {
        return toAjax(courseService.addCourseStudent(courseId, employeeIds));
    }

    /**
     * 移除课程学员
     */
    @SaCheckPermission("hrTraining:course:edit")
    @DeleteMapping("/{courseId}/student")
    public R<Void> removeStudent(@PathVariable Long courseId, @RequestBody List<Long> employeeIds) {
        return toAjax(courseService.removeCourseStudent(courseId, employeeIds));
    }

    /**
     * 开始学习
     */
    @SaCheckPermission("hrTraining:course:start")
    @GetMapping("/start/{courseId}")
    public R<Void> start(@NotNull(message = "主键不能为空") @PathVariable Long courseId) {
        return toAjax(courseService.startCourseDetail(courseId));
    }


}
